﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using MM.Entities;
using MM.Business;

namespace MM
{
    public partial class frmDuyetPhieuCK : Form
    {
        string _sConn = "";
        string _nguoitao = "";
        string _matau = "";
        List<CHI_TIET_PCK> lstCT = new List<CHI_TIET_PCK>();
        PHIEU_CHUYEN_KHO objPCK = new PHIEU_CHUYEN_KHO();
        public frmDuyetPhieuCK(String sConn)
        {
            InitializeComponent();
            _sConn = sConn;
            _nguoitao = frmDangNhap.objTaiKhoan._soCMND;
            _matau = frmDangNhap.objTau._tenTau2KiTu;

            this.HienthiPHIEU_CHUYEN_KHO_MAX(_matau);
            this.HienthiCHI_TIET_PCK();
        }

        private void HienthiPHIEU_CHUYEN_KHO_MAX(string matau)
        {
            clsPHIEU_CHUYEN_KHO pxhCtrl = new clsPHIEU_CHUYEN_KHO(_sConn);
            List<PHIEU_CHUYEN_KHO> lst = new List<PHIEU_CHUYEN_KHO>();
            lst = pxhCtrl.usp_SelectPHIEU_CHUYEN_KHO_MAX(matau);
            if (lst.Count > 0)
            {
                objPCK = new PHIEU_CHUYEN_KHO();
                objPCK = lst[0];
                this.txtNgayThucHien.Text = lst[0].NgayXuat.Day.ToString() + "/" + lst[0].NgayXuat.Month.ToString() + "/" + lst[0].NgayXuat.Year.ToString();
                this.txtNguoiThucHien.Text = lst[0].NguoiXuat;
                if (lst[0].NgayExport.HasValue)
                {
                    this.txtNgayDuyet.Text = ((DateTime)lst[0].NgayExport).Day.ToString() + "/" + ((DateTime)lst[0].NgayExport).Month.ToString() + "/" + ((DateTime)lst[0].NgayExport).Year.ToString();
                }
                else
                {
                    this.txtNgayDuyet.Text = "";
                }
                this.txtSoPhieuCK.Text = lst[0].MaPhieuChuyenKho;
                if (lst[0].SoLanExport > 0)
                {
                    this.cmdDuyet.Enabled = false;
                    this.txtGhiChu.Text = lst[0].GhiChu;
                    this.cmdXuatLai.Enabled = true;
                }
                else
                {
                    this.cmdDuyet.Enabled = true;
                    this.cmdXuatLai.Enabled = false;
                }
            }
        }

        private void HienthiPHIEU_CHUYEN_KHO(string maphieuchuyenkho)
        {
            clsPHIEU_CHUYEN_KHO pxhCtrl = new clsPHIEU_CHUYEN_KHO(_sConn);
            List<PHIEU_CHUYEN_KHO> lst = new List<PHIEU_CHUYEN_KHO>();
            lst = pxhCtrl.usp_SelectPHIEU_CHUYEN_KHO(maphieuchuyenkho);
            if (lst.Count > 0)
            {
                objPCK = new PHIEU_CHUYEN_KHO();
                objPCK = lst[0];
                this.txtNgayThucHien.Text = lst[0].NgayXuat.Day.ToString() + "/" + lst[0].NgayXuat.Month.ToString() + "/" + lst[0].NgayXuat.Year.ToString();
                this.txtNguoiThucHien.Text = lst[0].NguoiXuat;
                if (lst[0].NgayExport.HasValue)
                {
                    this.txtNgayDuyet.Text = ((DateTime)lst[0].NgayExport).Day.ToString() + "/" + ((DateTime)lst[0].NgayExport).Month.ToString() + "/" + ((DateTime)lst[0].NgayExport).Year.ToString();
                }
                else
                {
                    this.txtNgayDuyet.Text = "";
                }
                this.txtSoPhieuCK.Text = lst[0].MaPhieuChuyenKho;
                if (lst[0].SoLanExport > 0)
                {
                    this.cmdDuyet.Enabled = false;
                    this.txtGhiChu.Text = lst[0].GhiChu;
                    this.cmdXuatLai.Enabled = true;
                }
                else
                {
                    this.cmdDuyet.Enabled = true;
                    this.cmdXuatLai.Enabled = false;
                }
            }
        }

        private void HienthiCHI_TIET_PCK()
        {
            clsCHI_TIET_PCK ctpxhCtrl = new clsCHI_TIET_PCK(_sConn);

            lstCT = ctpxhCtrl.usp_SelectCHI_TIET_PCK_ByMaPhieuChuyenKho(this.txtSoPhieuCK.Text.Trim());
            lstCT.RemoveAll(delegate(CHI_TIET_PCK x) { return x.TinhTrang == 1; });
            this.gridControl1.DataSource = lstCT;
        }

        private void cmdDuyet_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.txtGhiChu.Text.Trim() == "")
                {
                    MessageBox.Show("Vui lòng nhập diễn giải cho đợt CHUYỂN KHO");
                    return;
                }
                string filepath = "";
                clsTHAM_SO tsCtrl = new clsTHAM_SO(_sConn);
                THAM_SO tsObj = new THAM_SO();
                tsObj = tsCtrl.SelectTHAM_SO("vDuyetPCK");

                if (tsObj != null && tsObj.DaXoa == 0)
                {
                    filepath = tsObj.GiaTri;
                }

                if (filepath == "")
                {
                    FolderBrowserDialog dg = new FolderBrowserDialog();
                    dg.ShowDialog();
                    filepath = dg.SelectedPath;
                }
                if (filepath != "")
                {
                    clsPHIEU_CHUYEN_KHO pxhCtrl = new clsPHIEU_CHUYEN_KHO(_sConn);
                    PHIEU_CHUYEN_KHO obj = new PHIEU_CHUYEN_KHO();
                    obj.MaPhieuChuyenKho = this.txtSoPhieuCK.Text.Trim();
                    obj.NgayExport = DateTime.Today;
                    obj.NguoiExport = _nguoitao;
                    obj.NgayXuat = DateTime.Today;
                    obj.NguoiXuat = _nguoitao;                 
                    obj.GhiChu = this.txtGhiChu.Text;

                    if (obj.MaPhieuChuyenKho.Length == 10)
                    {
                        int kq = pxhCtrl.usp_UpdatePHIEU_CHUYEN_KHO_Duyet(obj);
                        if (kq == 1)
                        {
                            // xuat file Csv
                            string y = DateTime.Today.Year.ToString();
                            string m = DateTime.Today.Month.ToString();
                            string d = DateTime.Today.Day.ToString();
                            m = Convert.ToString("0" + m).Substring(m.Length - 1, 2);
                            d = Convert.ToString("0" + d).Substring(d.Length - 1, 2);
                            filepath = filepath + "\\" + obj.MaPhieuChuyenKho + "_" + y + m + d + ".csv";
                            // Xuat file Excel GI
                            pxhCtrl.CsvExportPHIEU_CHUYEN_KHO(obj, lstCT, filepath);

                            // thong bao cho nguoi dung
                           // MessageBox.Show("DUYỆT THÀNH CÔNG!" + "\n" + "GỬI EMAIL ĐÍNH KÈM FILE TẠI: " + filepath + " VỀ ĐỊA CHỈ: vmm@vitranschart.com.vn");
                            this.lblThongBao.Text = "DUYỆT THÀNH CÔNG!" + "\r\n" + "GỬI EMAIL ĐÍNH KÈM FILE TẠI: " + filepath + " VỀ ĐỊA CHỈ: vmm@vitranschart.com.vn";
                            this.cmdDuyet.Enabled = false;
                            this.cmdXuatLai.Enabled = true;
                        }
                        else
                            MessageBox.Show("DUYỆT KHÔNG THÀNH CÔNG");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void txtSoPhieuCK_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (this.txtSoPhieuCK.Text.Trim().Length == 10)
                {
                    this.HienthiPHIEU_CHUYEN_KHO(this.txtSoPhieuCK.Text.Trim());
                    this.HienthiCHI_TIET_PCK();                   
                }
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {
            frmTimPhieuCK f = new frmTimPhieuCK(_sConn);
            f.chonPCK += new frmTimPhieuCK.ChonPhieuChuyenKho(GetPhieuChuyenKho);
            f.Show(this);
        }

        private void GetPhieuChuyenKho(object sender, ChonPhieuCKEventArgs e)
        {
            string mapck = e.MaPhieuChuyenKho;

            this.HienthiPHIEU_CHUYEN_KHO(mapck);
            this.HienthiCHI_TIET_PCK();
        }

        private void frmDuyetPhieuCK_Load(object sender, EventArgs e)
        {
            Common.SetColor(this);
            this.lblThongBao.BackColor = this.BackColor;
        }

        private void cmdXuatLai_Click(object sender, EventArgs e)
        {
            try
            {
                if (objPCK.SoLanExport <= 0)
                {
                    MessageBox.Show("XUẤT FILE KHÔNG THÀNH CÔNG!\nPHIẾU CHƯA ĐƯỢC DUYỆT");
                    return;
                }

                if (this.txtGhiChu.Text.Trim() == "")
                {
                    MessageBox.Show("Vui lòng nhập diễn giải cho đợt CHUYỂN KHO");
                    return;
                }
                string filepath = "";
                clsTHAM_SO tsCtrl = new clsTHAM_SO(_sConn);
                THAM_SO tsObj = new THAM_SO();
                tsObj = tsCtrl.SelectTHAM_SO("vDuyetPCK");

                if (tsObj != null && tsObj.DaXoa == 0)
                {
                    filepath = tsObj.GiaTri;
                }

                if (filepath == "")
                {
                    FolderBrowserDialog dg = new FolderBrowserDialog();
                    dg.ShowDialog();
                    filepath = dg.SelectedPath;
                }
                if (filepath != "")
                {
                    objPCK.SoLanExport += 1;
                    clsPHIEU_CHUYEN_KHO pxhCtrl = new clsPHIEU_CHUYEN_KHO(_sConn);

                    if (objPCK.MaPhieuChuyenKho.Length == 10)
                    {
                        int kq = pxhCtrl.usp_UpdatePHIEU_CHUYEN_KHO_Duyet(objPCK);
                        if (kq == 1)
                        {
                            // xuat file Csv
                            string y = DateTime.Today.Year.ToString();
                            string m = DateTime.Today.Month.ToString();
                            string d = DateTime.Today.Day.ToString();
                            m = Convert.ToString("0" + m).Substring(m.Length - 1, 2);
                            d = Convert.ToString("0" + d).Substring(d.Length - 1, 2);
                            filepath = filepath + "\\" + objPCK.MaPhieuChuyenKho + "_" + y + m + d + ".csv";
                            // Xuat file Excel GI
                            pxhCtrl.CsvExportPHIEU_CHUYEN_KHO(objPCK, lstCT, filepath);

                            // thong bao cho nguoi dung
                           
                            this.lblThongBao.Text = "DUYỆT LẠI FILE THÀNH CÔNG!" + "\r\n" + "GỬI EMAIL ĐÍNH KÈM FILE TẠI: " + filepath + " VỀ ĐỊA CHỈ: vmm@vitranschart.com.vn";
                            this.cmdDuyet.Enabled = false;
                            this.cmdXuatLai.Enabled = true;
                        }
                        else
                            MessageBox.Show("XUẤT LẠI FILE KHÔNG THÀNH CÔNG");
                    }
                }
            }
            catch { }
        }
    }
}